<?php

class Api extends CComponent {

    public static function getBusStation($iBusId, $bIsDirection) {
        $criterial = new CDbCriteria();

        $criterial->addCondition('bus_id = ' . (int) $iBusId);

        if ($bIsDirection) {
            $criterial->addCondition('direction = ' . BusStation::DIRECTION_TRUE);
        } else {
            $criterial->addCondition('direction = ' . BusStation::DIRECTION_FALSE);
        }

        $models = BusStation::model()->with('station')->findAll($criterial);

        $aJson = array();
        foreach ($models as $model) {
            $aJson[] = array(
                'bus_station_id' => $model->bus_station_id,
                'station_id' => $model->station_id,
                'name' => $model->station->name,
                'latitude' => $model->station->latitude,
                'longitude' => $model->station->longitude,
                'ordering' => $model->ordering,
            );
        }
        
        return $aJson;
    }

    public static function getBusPolyline($iBusId, $bIsDirection) {
        $criterial = new CDbCriteria;
        if ($bIsDirection) {
            $criterial->addCondition('direction = ' . Polyline::DIRECTION_TRUE);
        } else {
            $criterial->addCondition('direction = ' . Polyline::DIRECTION_FALSE);
        }

        $criterial->addCondition('bus_id = ' . (int) $iBusId);
        $modelPolyline = Polyline::model()->find($criterial);

        $aJsonPolyline = CJSON::decode($modelPolyline->data);
        
        return $aJsonPolyline;
    }

}